import {http} from "@/plugins/net";
import {Message} from "element-ui";

/**
 * 根据多个字典类型获取各个字典类型的字典数据
 * @param type
 * @returns {Promise<AxiosResponse<any>>}
 */
const getDictData =  function getDictData(dictTypes) {
    if (!dictTypes) {
        return null;
    }
    let res = {}
    if (dictTypes && dictTypes instanceof Array) {
        dictTypes.forEach(dictType => {
            let url = `/dictData/type/data?dictType=${dictType}`;

            http(url,'get',null).then(
                success => {
                    if (success) {
                        for (let key in success) {
                            res[key] = success[key]
                        }
                    } else {
                        Message.error(`字典类型 【${dictType}】未定义或者没有对应的数据，或者被禁用！`);
                        return null
                    }
                }
            )
        })
    } else {
        throw new Error("dictType require Array,but apply to string")
    }
    return res
}

export {getDictData}


/**
 * 根据字典类型获取字典数据
 * @param type
 * @returns {Promise<AxiosResponse<any>>}
 */
export const getDictDataByType = async (type) => {
    let url = `/dictData/type/data?dictType=${type}`
    const res = await http(url,'get',null)
    if (res) {
        return res
    }
}

/**
 * 查询字典类型
 * @param searchData
 * @param currPage
 * @param pageSize
 * @returns {Promise<AxiosResponse<any>>}
 */
export const listDictType = async (searchData,currPage,pageSize) => {
    let url = '/sysDictType/list?1=1'
    if (searchData && searchData.dictName) {
        url = url + `&dictName=` + searchData.dictName
    }
    if (searchData && searchData.dictType) {
        url = url + `&dictType=` + searchData.dictType
    }
    if (currPage) {
        url += `&currentPage=${currPage}`
    }
    if (pageSize) {
        url += `&pageSize=${pageSize}`
    }
    const res = await http(url,'get',null)
    if (res) {
        return res
    }
}

/**
 * 添加字典类型
 * @param dictType
 * @returns {Promise<AxiosResponse<any>>}
 */
export const addDictType = async (dictType) => {
    let url = `/sysDictType/add`
    const res = await http(url,'post',dictType)
    return res
}

/**
 * 检查字典类型是否已经存在
 * @param dictType
 * @returns {Promise<AxiosResponse<any>>}
 */
export const checkDictTypeExist = async (dictType) => {
    let url = `/sysDictType/check?dictType=${dictType}`
    const res = await http(url,'get',null)
    if (res) {
        return res
    }
}

/**
 * 更新字典类型
 * @param dictType
 * @returns {Promise<AxiosResponse<any>>}
 */
export const updateDictType = async (dictType) => {
    let url = `/sysDictType/modify`
    const res = await http(url,'put',dictType)
    if (res) {
        return res
    }
}

/**
 * 添加字典项
 * @param dictData
 * @returns {Promise<void>}
 */
export const addDictData = async (dictData) => {
    let url = `/dictData/add`
    await http(url,'post',dictData)
}

/**
 * 删除字典项
 * @param id
 * @returns {Promise<void>}
 */
export const deleteDictData = async (dictData) => {
    let url = `/dictData/delete`
    await http(url,'post',dictData)
}

/**
 * 更新字典项
 * @param dictData
 * @returns {Promise<void>}
 */
export const updateDictData = async (dictData) => {
    let url = `/dictData/update`
    await http(url,'put',dictData)
}

export const deleteDictType = async (typeIds) => {
    let url = `/sysDictType/delete?1=1`
    if (typeIds || typeIds instanceof Array) {
        typeIds.forEach(item => {
            url += `&ids=${item}`
        })

        await http(url,'delete')
    }
}